package listeners

import (
	"blockchains-aio/http"
	"blockchains-aio/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
)

func RegisterImage(router *gin.Engine) {
	router.MaxMultipartMemory = 8 << 10 // 8 KB
	router.POST("/image", doDockPullImage)
}

func doDockPullImage(ctx *gin.Context) {
	http.WorkThroughPostFormInitializationInGin(ctx)
	imageName := ctx.PostForm("imageName")
	if len(imageName) == 0 {
		ctx.JSON(400, "found imageName in the form")
		return
	}
	log.Printf("imageName: %s", imageName)

	out, err := util.DockerPullImage(imageName)
	if err != nil {
		ctx.JSON(400, fmt.Sprintf("Failed to pull image: %v", err))
	}
	ctx.JSON(200, string(out))
}
